package cn.controller;

import cn.dto.Query;
import cn.entity.*;
import cn.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.List;

import static cn.dto.StaticData.*;

@Controller
public class PageController{
    @Autowired
    private PageService pageService;

    @RequestMapping(value = "Homepage",method = RequestMethod.GET)
    public String index(Model model){
        List<Banner> bannerList = pageService.getBannerList(BANNER_LIMIT);
        List<News> newsList = pageService.getNewsList(NEWS_LIMIT);
        Introduction introduction = pageService.getIntroduction();
        model.addAttribute("bannerList",bannerList);
        model.addAttribute("newsList",newsList);
        model.addAttribute("introduction",introduction);
        return "Homepage";
//        return "index";
    }

    @RequestMapping(value = "introduction",method = RequestMethod.GET)
    public String introduction(Model model){
        Introduction introduction = pageService.getIntroduction();
        model.addAttribute("introduction",introduction);
//        return "introduction";
        return "index";
    }
    @RequestMapping(value = "Members",method = RequestMethod.GET)
    public String member(Model model,@RequestParam(defaultValue = "1") String id,Query query){
        List<SubMenu> subMenuList = pageService.getSubMenuList("member");
        List<Member> memberList = pageService.getMemberListByMenuId(id,query);
        SubMenu subMenu = getCurrentSubMenu(subMenuList,id);
        model.addAttribute("subMenu",subMenu);
        model.addAttribute("subMenuList",subMenuList);
        model.addAttribute("memberList",memberList);
        return "Members";
//        return "index";
    }

    private SubMenu getCurrentSubMenu(List<SubMenu> subMenuList, String id) {
        for(SubMenu subMenu:subMenuList){
            if(subMenu.getId().toString().equals(id)){
                return subMenu;
            }
        }
        return null;
    }

    @RequestMapping(value = "personal",method = RequestMethod.GET)
    public String getMember(Model model,String id){
        List<SubMenu> subMenuList = pageService.getSubMenuList("member");
        Member member = pageService.getMember(id);
        SubMenu subMenu = getCurrentSubMenu(subMenuList, String.valueOf(member.getSubMenuId()));
        List<MemberMsg> memberMsgList = pageService.getMemberMsgByMemId(id);
        for(MemberMsg memberMsg:memberMsgList){
            switch(memberMsg.getType()){
                case 0://经历
                    model.addAttribute("experience",memberMsg.getMsg());
                    break;
                case 1://荣誉称号
                    model.addAttribute("honoraryTitle",memberMsg.getMsg());
                    break;
                case 2://科研项目
                    model.addAttribute("project",memberMsg.getMsg());
                    break;
                case 3://成果
                    model.addAttribute("achievement",memberMsg.getMsg());
                    break;
            }
        }
        model.addAttribute("subMenu",subMenu);
        model.addAttribute("subMenuList",subMenuList);
        model.addAttribute("member",member);

        return "personal";
//        return "index";
    }
    @RequestMapping(value = "Facilities",method = RequestMethod.GET)
    public String getFacilities(Model model, Query query){
        List<SubMenu> subMenuList = pageService.getSubMenuList("facilities");
        List<Machine> machineList = pageService.getMachineList(query);
        model.addAttribute("subMenuList",subMenuList);
        model.addAttribute("facilitiesList",machineList);
        return "Facilities";
//        return "index";
    }
    @RequestMapping(value = "equipment",method = RequestMethod.GET)
    public String getEquipment(Model model,String id){
        List<SubMenu> subMenuList = pageService.getSubMenuList("equipment");
        Machine machine = pageService.getMachine(id);
        model.addAttribute("subMenuList",subMenuList);
        model.addAttribute("equipment",machine);
        return "equipment";
//        return "index";
    }
    @RequestMapping(value = "news",method = RequestMethod.GET)
    public String getNews(Model model, Query query){
        List<SubMenu> subMenuList = pageService.getSubMenuList("news");
        List<News> newsList = pageService.getNewsList(query);
        model.addAttribute("subMenuList",subMenuList);
        model.addAttribute("newsList",newsList);
        return "news";
//        return "index";
    }

    /**
     * 研究成果
     */
    @RequestMapping(value = "Publications",method = RequestMethod.GET)
    public String getPublications(Model model, Query query, @RequestParam(value = "year",defaultValue = "2016") String year){
        List<Integer> yearList = pageService.getPublicationsYearList();
        List<Paper> paperList = pageService.getPaperListByYear(query,year);
        model.addAttribute("currentSubMenu",year);
        model.addAttribute("yearList",yearList);
        model.addAttribute("publicationsList",paperList);
        return "Publications";
//        return "index";
    }

    /**
     * 招聘启事
     */
    @RequestMapping(value = "Recruitment",method = RequestMethod.GET)
    public String getRecruitment(Model model,@RequestParam(defaultValue = "7") String id){
        List<SubMenu> subMenuList = pageService.getSubMenuList("contact");
        Contact contact = pageService.getContactByMenuId(id);
        model.addAttribute("subMenuList",subMenuList);
        model.addAttribute("recruitment",contact);
        return "Recruitment";
//        return "index";
    }

    /**
     * 研究内容
     */
    @RequestMapping(value = "Research",method = RequestMethod.GET)
    public String getResearch(Model model,@RequestParam(defaultValue = "9") String id){
        List<SubMenu> subMenuList = pageService.getSubMenuList("research");
        Research research = pageService.getResearchByMenuId(id);
        model.addAttribute("subMenuList",subMenuList);
        model.addAttribute("research",research);
        return "Research";
//        return "index";
    }
    /**
     * 学术奖励
     */
    @RequestMapping(value = "Awards",method = RequestMethod.GET)
    public String getAwards(Model model,@RequestParam(defaultValue = "12") String id){
        List<SubMenu> subMenuList = pageService.getSubMenuList("awards");
        AcademicAwards awards = pageService.getAwardsByMenuId(id);
        model.addAttribute("subMenuList",subMenuList);
        model.addAttribute("awards",awards);
        return "Awards";
//        return "index";
    }
    /**
     * 学术活动
     */
    @RequestMapping(value = "Academic",method = RequestMethod.GET)
    public String getAcademic(Model model,Query query){
//        List<SubMenu> subMenuList = pageService.getSubMenuList("academic");
        List<AcademicActivity> activityList = pageService.selectAcademicList(query);
//        model.addAttribute("subMenuList",subMenuList);
        model.addAttribute("activity",activityList);
        return "Academic";
//        return "index";
    }
    /**
     * 学术活动
     */
    @RequestMapping(value = "Cooperation",method = RequestMethod.GET)
    public String getCooperation(Model model,Query query){
//        List<SubMenu> subMenuList = pageService.getSubMenuList("academic");
        List<Cooperation> cooperationList = pageService.selectCooperationList(query);
//        model.addAttribute("subMenuList",subMenuList);
        model.addAttribute("cooperationList",cooperationList);
        return "Cooperation";
//        return "index";
    }
}
